package com.unlcn.erp.hyc.interfaces.api.controller;

import cn.huiyunche.commons.domain.ResultDTO;
import cn.huiyunche.commons.exception.BusinessException;
import com.unlcn.erp.hyc.interfaces.backend.enums.CallHYCUrlTypeEnum;
import com.unlcn.erp.hyc.interfaces.backend.service.ExecptionDisposeService;
import com.unlcn.erp.hyc.interfaces.base.model.bo.HycExceptionMsgBo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by zhiche024 on 2017/9/5.
 */
@Controller
@RequestMapping("/callHYCExcepton")
public class CallHYCExceptionController {
    private static final Logger LOGGER = LoggerFactory.getLogger(CallHYCExceptionController.class);

    @Autowired
    private ExecptionDisposeService execptionDisposeService;


    @RequestMapping(value = "/showExceptionList", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<List<HycExceptionMsgBo>> getExceptionList(){
        ResultDTO<List<HycExceptionMsgBo>> result = new ResultDTO<>(true, null, "查询异常成功");
        try{
            List<HycExceptionMsgBo> msgList =  execptionDisposeService.getUndisposedExceptionList();
            msgList.forEach(HycExceptionMsgBo -> {
                if(HycExceptionMsgBo.getExceptionType() == CallHYCUrlTypeEnum.HYC_ORDERDETAILS.getValue()){
                    HycExceptionMsgBo.setExceptionTypeMsg("获取订单明细异常");
                }
                if(HycExceptionMsgBo.getExceptionType() == CallHYCUrlTypeEnum.HYC_WAYBILLDETAILS.getValue()){
                    HycExceptionMsgBo.setExceptionTypeMsg("获取运单明细异常");
                }
            });
            result.setData(msgList);
        }catch (Exception e){
            LOGGER.error("TmsPickUpController.getExcepList error: {}", e);
            result.setSuccess(false);
            result.setMessage("查询异常列表成功");
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    /**
     *  根据错误执行运单
     * @param code
     * @return
     */
    @RequestMapping(value = "/doDisposeExeByCode", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Boolean>  doDisposeExeByCode(@RequestParam("exceptionType") int exceptionType,@RequestParam("code") String code){
        LOGGER.info("callHYCExcepton.doDisposeExeByCode param:{}.");
        ResultDTO<Boolean> result = new ResultDTO<>(true, null, "执行成功");
        try{
            Boolean blnRtn = execptionDisposeService.doDisposeExe(exceptionType,code);
            result.setData(blnRtn);
        }catch (Exception e){
            LOGGER.error("callHYCExcepton.doDisposeExeByCode error: {}", e);
            result.setSuccess(false);
            result.setMessage("执行失败");
            throw new BusinessException(e.getMessage());
        }
        return result;
    }


}
